package com.zh.common.config;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
//import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Description: swagger的配置类
 * @ClassName SwaggerConfig
 * @date: 2020.12.08 15:24
 * @Author: zhanghang
 */
@Slf4j
@Configuration // 配置类
@EnableSwagger2 // 开启swagger文档
//@EnableSwaggerBootstrapUI // 开启doc.html文档
public class SwaggerConfig {
    public static final String SWAGGER_SCAN_USERCENTER_PACKAGE = "com.zh";
    public static final String SWAGGER_GROUPNAME_USERCENTER = "张行文档中心";

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2) //
                .groupName(SWAGGER_GROUPNAME_USERCENTER) //
                .apiInfo(apiInfo()) //
//                .globalOperationParameters(setHeaderToken()) //
                .select() //
                .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_USERCENTER_PACKAGE)) // //.paths(PathSelectors.any()) // 对所有的路径进行监控
                .build();
//                .securitySchemes(security()); // 主要关注点--统一填写一次token
    }


    /**
     * @title 创建该API的基本信息
     *
     * @url: http://项目实际地址/swagger-ui.html
     *
     * @return
     */
    @SuppressWarnings("deprecation")
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("张行的小项目 RESTful APIs") //
                .description("基础平台 RESTful 风格的接口文档,内容详细,极大的减少了前后端的沟通成本,同时确保代码与文档保持高度一致,极大的减少维护文档的时间.") //
                .termsOfServiceUrl("") //
                .contact("MAGICGREEK") //
                .version("1.0.0") //
                .build();
    }
//
//    private List<Parameter> setHeaderToken() {
//        ParameterBuilder uidPar = new ParameterBuilder();
//        List<Parameter> pars = new ArrayList<Parameter>();
//        uidPar.name("uid").description("登录用户id").modelRef(new ModelRef("string")).parameterType("header").required(true).build();
//        pars.add(uidPar.build());
//        return pars;
//    }
//
//    private List<ApiKey> security() {
//        List<ApiKey> apiKeyList = new ArrayList<ApiKey>();
//        apiKeyList.add(new ApiKey("token", "Authorization", "header"));
//        apiKeyList.add(new ApiKey("userInfo", "userInfo", "header"));
//        return apiKeyList;
//    }
//
//    private List<SecurityContext> securityContexts() {
//        List<SecurityContext> securityContexts = new ArrayList<>();
//        securityContexts.add(SecurityContext.builder().securityReferences(defaultAuth()).forPaths(PathSelectors.regex("^(?!auth).*$")).build());
//        return securityContexts;
//    }
//
//    List<SecurityReference> defaultAuth() {
//        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
//        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
//        authorizationScopes[0] = authorizationScope;
//        List<SecurityReference> securityReferences = new ArrayList<>();
//        securityReferences.add(new SecurityReference("token", authorizationScopes));
//        return securityReferences;
//    }
}
